﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;
using Cn.Cellula.Client.ControlHelper;
using Cn.Edwon.Web;
using Cn.Edwon.Web.Support;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using Cn.Cellula.Model;
using Cn.Cellula.Biz;
using Cn.Edwon.Biz;
using Cn.Cellula.Model.Product;
using Cn.Cellula.Client;
namespace Cn.Cellula.Website.Admin.Product.Group {
    public partial class Bind : PageSupport {
        IProductBiz biz = BizFactory.Get<IProductBiz>();
        protected void Page_Load(object sender, EventArgs e) {
            if (!Page.IsPostBack) {
                InitForm();
                LoadGroupedProduct();
                LoadProduct();
            }
        }

        private void InitForm() {
            String hql = " from TProductGroupEO eo where eo.OwnerKey='" + this.OwnerKey + "' ";
            hql += " order by eo.LastDate desc ";
            IList<TProductGroupEO> list = biz.Find<TProductGroupEO>(hql);
            this.ddl_Group.DataValueField = "GroupKey";
            this.ddl_Group.DataTextField = "GroupName";
            this.ddl_Group.DataSource = list;
            this.ddl_Group.DataBind();
            try {
                this.ddl_Group.SelectedValue = Request.QueryString["gid"];
            } catch { }


            ICategoryBiz cbiz = BizFactory.Get<ICategoryBiz>();
            this.ddl_Category.DataTextField = "Text";
            this.ddl_Category.DataValueField = "Value";
            this.ddl_Category.DataSource = cbiz.LoadCatalog2ListItem(this.OwnerKey, CodeTypeDef.Category.Product, "..", null, true);
            this.ddl_Category.DataBind();
            this.ddl_Category.Items.Insert(0, new ListItem(TxtDef.DDLTextAll, ""));

        }
        private String BuildSearch() {
            String hql = "";
            if (!String.IsNullOrEmpty(this.txt_Model.Text)) {
                hql += " and eo.ProductModel like '%" + this.txt_Model.Text + "%' ";
            }
            if (!String.IsNullOrEmpty(this.txt_Name.Text)) {
                hql += " and eo.ProductName like '%" + this.txt_Name.Text + "%' ";
            }
            if (!String.IsNullOrEmpty(this.txt_ProductNo.Text)) {
                hql += " and eo.ProductNo like '%" + this.txt_ProductNo.Text + "%' ";
            }
            if (!String.IsNullOrEmpty(this.ddl_Category.SelectedValue)) {
                hql += " and eo.CategoryKey='" + this.ddl_Category.SelectedValue + "' ";
            }
            return hql;
        }
        private void LoadGroupedProduct() {
            String hql = " from VProductWithGroupEO eo where eo.OwnerKey='" + this.OwnerKey + "' ";
            hql += " and eo.GroupKey='" + this.ddl_Group.SelectedValue + "'";
            hql += BuildSearch();
            hql += " order by eo.LastDate desc ";
            IList<VProductWithGroupEO> list = biz.Find<VProductWithGroupEO>(hql);
            this.gvList.DataSource = list;
            this.gvList.DataBind();
        }
        private void LoadProduct() {

            String hql = " from VProductEditListEO eo where eo.OwnerKey='" + this.OwnerKey + "' ";
            String _hql = " from VProductInGroupEO eo where eo.GroupOwnerKey='" + this.OwnerKey + "' and eo.GroupKey='" + this.ddl_Group.SelectedValue + "'";
            IList<VProductInGroupEO> gList = biz.Find<VProductInGroupEO>(_hql);
            if (gList != null && gList.Count>0) {
                hql += " and ( ";
                for (int i=0;i<gList.Count;i++){
                    VProductInGroupEO g = gList[i];
                    if (i > 0) {
                        hql += " and ";
                    }
                    hql += "(eo.ProductId <>'"+g.ProductKey+"' )";
                }
                hql += " ) ";
            }
            hql += BuildSearch();
            hql += " order by eo.LastDate desc ";
            IList<VProductEditListEO> list = biz.Find<VProductEditListEO>(hql);
            this.gvProduct.DataSource = list;
            this.gvProduct.DataBind();
        }

        protected void gvProduct_PageIndexChanging(object sender, GridViewPageEventArgs e) {
            this.gvProduct.PageIndex = e.NewPageIndex;
            this.LoadProduct();
        }

        protected void gvList_PageIndexChanging(object sender, GridViewPageEventArgs e) {
            this.gvProduct.PageIndex = e.NewPageIndex;
            this.LoadGroupedProduct();
        }

        protected void btn_QueryGrouped_Click(object sender, EventArgs e) {
            this.LoadGroupedProduct();
            this.LoadProduct();
        }

        protected void btn_Remove_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvList, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            if (keys != null && keys.Count > 0) {
                biz.BindProductOutGroup(keys, this.ddl_Group.SelectedValue);
                this.LoadGroupedProduct();
                this.LoadProduct();
            } else {
                SiteInfo.Send(Page, TxtDef.ERROR, TxtDef.NoMultiSelect);
                return;
            }

        }

        protected void btn_Query_Click(object sender, EventArgs e) {
            this.LoadProduct();
        }

        protected void btn_Add_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvProduct, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            if (keys != null && keys.Count > 0) {
                biz.BindProductInGroup(keys, this.ddl_Group.SelectedValue);
                this.LoadGroupedProduct();
                this.LoadProduct();
            } else {
                SiteInfo.Send(Page, TxtDef.ERROR, TxtDef.NoMultiSelect);
                return;
            }

        }
    }
}
